
/**
 * 获取地址栏参数
 */
 export function getUrlParams() {
  const url = window.location.href;
  if ("undefined" == url) {
    return {};
  }
  let paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
  let paraObj = {};
  if (paraString == url) {
    return "";
  }
  for (let i = 0; i < paraString.length; i++) {
    let j = paraString[i];
    paraObj[j.substring(0, j.indexOf("="))] = j.substring(
      j.indexOf("=") + 1,
      j.length
    );
  }
  
  return paraObj;
}





export const setLocal = function(key, value) {
  window.localStorage.setItem(key, value);
};
export const getLocal = function(key) {
  return window.localStorage.getItem(key);
};

/**
 * @param {Object} obj - {key,value}
 */
export const saveToLocal = function(obj) {
  for (let key in obj) {
    let value = obj[key];
    setLocal(key, value);
  }
};

/**
 * @param {Array} keys
 */
export const removeLocal = function(keys) {
  keys.forEach((key) => setLocal(key, ""));
};

export function loginToNative({ indexCollection = 0, zbInfo }) {
  const { centralToken, phone, isInside } = window.userInfo;
  const now = Date.now();
  console.log(indexCollection);
  const info = indexCollection == 1 ? zbInfo : "[]";
  const { endTime = "", productId = "" } = window.userInfo.order || {};
  window.prompt(
    "",
    `up://["web_user_login",${now},{"type":"login","data":{"token":"${centralToken}","time":"${window.systemTime}","logintype":"0","openid":"${phone}","indexcollection":${indexCollection},"expiretime":"${endTime}","productid":"${productId}","isInside":${isInside},"zbInfo":${info}}}]`
  );
}

var CryptoJS = require("crypto-js");
export function encryptByDES(message, key = "T137SRpGil0=") {
  var words = CryptoJS.enc.Base64.parse(key);
  var encrypted = CryptoJS.DES.encrypt(message, words, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  });

  return encrypted.ciphertext
    .toString(CryptoJS.enc.Base64)
    .replace(/\//g, ",")
    .replace(/=/g, "_")
    .replace(/\+/g, ".");
}
